import json
import re
import time
import os
from dbhelper import DBHelper

IMG_BASE_URL = "https://neimengweixin.cdn.xianyugame.com/food/"

mysql_db = DBHelper()



# sql = ''' insert into user(openid, shop_id) values (%s, %s) '''
# params = ('aaa', 1)

# try:
# 	mysql_db.insert(sql, *params)
# except Exception as e:
# 	print(e)



def get_shop_info():
	with open('shop_info.txt', 'r', encoding='utf-8') as f:
		shop_info_text = f.read()
		shop_info_text = re.sub('\'','\"',shop_info_text)
		shop_info_json = json.loads(shop_info_text)
		return shop_info_json
	return None

def get_shop_detil():
	with open('shop_detil.txt', 'r') as f:
		shop_detil_text = f.read()
		shop_detil_json = json.loads(shop_detil_text)
		return shop_detil_json
	return None

def process_shop_data(base_url):
	shop_info = get_shop_info()
	shop_detil = get_shop_detil()
	if shop_info and shop_detil:
		sql = ''' insert into shop(shop_name, shop_style, location, latitude, longitude, tel, collect_num, logo_url, region, area, notice) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) '''
		params = (shop_info['shopName'], shop_info['mainCategoryName'], shop_info['address'], shop_detil['shopGlat'], shop_detil['shopGlng'], shop_info['phoneNo'], shop_info['shopPower'], IMG_BASE_URL+shop_info['shopId']+'shop/logo.jpg', shop_info['mainRegionName'], shop_info['mainRegionName'], "")
		try:
			shop_id = mysql_db.insert(sql, *params)
			print(shop_id)
			process_dish_data(base_url, shop_info, shop_detil, shop_id)
		except Exception as e:
			print(e)


def get_dish_info():
	with open('new_dish_info.txt', 'r', encoding='utf-8') as f:
		dish_info_text = f.read()
		dish_info_text = re.sub('\'','\"',dish_info_text)
		dish_info_json = json.loads(dish_info_text)
		return dish_info_json
	return None


def process_dish_data(base_url, shop_info, shop_detil, shop_id):
	dir_names = []
	for f in os.listdir(base_url):
		if os.path.isdir(f):
			dir_names.append(f)

	for dir_name in dir_names:
		os.chdir(os.path.join(base_url, dir_name))
		dish_info = get_dish_info()
		# print(dish_info)
		# return
		if dish_info:
			sql = ''' insert into food(food_name, shop_id, food_img, desc_text, desc_img1, desc_img2, desc_img3, price, food_style, star_score, collect_num, create_time, latitude, longitude, food_flavor) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) '''
			desc_img1 = ''
			desc_img2 = ''
			desc_img3 = ''
			if os.path.exists(os.path.join(os.getcwd(), 'desc1.jpg')):
				desc_img1 = IMG_BASE_URL+shop_info['shopId']+'shop/'+dir_name+'/desc1.jpg'
			if os.path.exists(os.path.join(os.getcwd(), 'desc2.jpg')):
				desc_img1 = IMG_BASE_URL+shop_info['shopId']+'shop/'+dir_name+'/desc2.jpg'
			if os.path.exists(os.path.join(os.getcwd(), 'desc3.jpg')):
				desc_img1 = IMG_BASE_URL+shop_info['shopId']+'shop/'+dir_name+'/desc3.jpg'
			params = (dish_info['name'], shop_id, IMG_BASE_URL+shop_info['shopId']+'shop/'+dir_name+'/cover.jpg', "此菜品暂无描述信息", desc_img1, desc_img2, desc_img3, dish_info['price'], shop_info['mainCategoryName'], int(shop_info['score1'])/20, dish_info['collect'], time.time(), shop_detil['shopGlat'], shop_detil['shopGlng'], "")
			# print(params)
			# return
			try:
				dish_id = mysql_db.insert(sql, *params)
				print(dish_id)
			except Exception as e:
				print(e)
		


if __name__ == "__main__":
	process_shop_data(os.getcwd())
